<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $components = array('Session', 'RequestHandler');
	function index() {
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid comment', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}

	function add() {
		if($this->RequestHandler->isAjax()){
			$this->autoRender = false;
			$this->header('Pragma: no-cache');
			$this->header('Cache-control: no-cache');
			$this->header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		}
		$this->data['Comment']['user_id']    = isset($_POST['human'])?$_POST['human']:$this->Session->read('User.id');
		$this->data['Comment']['article_id'] = isset($_POST['article'])?$_POST['article']:'';
		$this->data['Comment']['contents']   = isset($_POST['content'])?htmlentities(trim($_POST['content']), ENT_QUOTES | ENT_IGNORE, "UTF-8"):'';
		$this->data['Comment']['visitor']    = isset($_POST['visitor'])?htmlentities($_POST['visitor'], ENT_QUOTES | ENT_IGNORE, "UTF-8"):'';
		$this->data['Comment']['visitor_contacts'] = isset($_POST['contact'])?htmlentities($_POST['contact'], ENT_QUOTES | ENT_IGNORE, "UTF-8"):'';
		//$this->data['Comment']['created'] = date('Y-m-d H:i:s');
		if (!empty($this->data)) {
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				echo json_encode(array('msg'=>true));
				exit;
			} else {
				echo json_encode(array('msg'=>false));
				exit;
			}
		}
	}

	function admin_index() {
		$this->_check();
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	function admin_view($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid comment', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}

	function admin_add() {
		$this->_check();
		if (!empty($this->data)) {
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The comment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
			}
		}
		$articles = $this->Comment->Article->find('list');
		$users = $this->Comment->User->find('list');
		$this->set(compact('articles', 'users'));
	}

	function admin_edit($id = null) {
		$this->_check();
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid comment', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The comment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
		$articles = $this->Comment->Article->find('list');
		$users = $this->Comment->User->find('list');
		$this->set(compact('articles', 'users'));
	}

	function admin_delete($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for comment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comment->delete($id)) {
			$this->Session->setFlash(__('Comment deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Comment was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>